package com.jhws.util.security;

import java.security.MessageDigest;

import javax.crypto.*;
import javax.crypto.spec.*;

import sun.misc.*;

public class CryptionData
{
	public static final String INIT_PWD = "888888";

	private String EncryptionString = "www.ccb.com.cn";

	private final byte[] EncryptionIV = "ABCDEFGH".getBytes();

	private final static String DES = "DES/CBC/PKCS5Padding";

	public CryptionData()
	{
	}

	public CryptionData(String EncryptionString)
	{
		this.EncryptionString = EncryptionString;
	}

	public byte[] EncryptionByteData(byte[] SourceData) throws Exception
	{
		byte[] retByte = null;

		byte[] EncryptionByte = EncryptionString.getBytes();
		DESKeySpec dks = new DESKeySpec(EncryptionByte);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey securekey = keyFactory.generateSecret(dks);

		IvParameterSpec spec = new IvParameterSpec(EncryptionIV);

		Cipher cipher = Cipher.getInstance(DES);

		cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);

		retByte = cipher.doFinal(SourceData);
		return retByte;
	}
	public byte[] DecryptionByteData(byte[] SourceData) throws Exception
	{
		byte[] retByte = null;

		byte[] EncryptionByte = EncryptionString.getBytes();
		DESKeySpec dks = new DESKeySpec(EncryptionByte);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey securekey = keyFactory.generateSecret(dks);

		IvParameterSpec spec = new IvParameterSpec(EncryptionIV);

		Cipher cipher = Cipher.getInstance(DES);

		cipher.init(Cipher.DECRYPT_MODE, securekey, spec);

		retByte = cipher.doFinal(SourceData);

		return retByte;
	}

	public String EncryptionStringData(String SourceData) throws Exception
	{
		String retStr = null;
		byte[] retByte = null;

		byte[] sorData = SourceData.getBytes();

		retByte = EncryptionByteData(sorData);

		BASE64Encoder be = new BASE64Encoder();
		retStr = be.encode(retByte);

		return retStr;
		//return SourceData;
	}

//	public String DecryptionStringData(String SourceData) throws Exception
//	{
//		String retStr = null;
//		byte[] retByte = null;
//
//		BASE64Decoder bd = new BASE64Decoder();
//		byte[] sorData = bd.decodeBuffer(SourceData);
//		retByte = DecryptionByteData(sorData);
//		retStr = new String(retByte);
//		return retStr;
//		//return SourceData;
//	}

	public static String MD5EncryptionStringData(String inStr){  
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            System.out.println(e.toString());  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }  
}
